local Utility = (function() local PARAM = RaycastParams.new() return { voxel_shade = function(normal, light_direction) return math.clamp(0.5 + (normal:Dot(light_direction) / 2), 0, 1) end, in_shadow = function(position, ignore) PARAM.FilterDescendantsInstances = {ignore} local sunDirection = game.Lighting:GetSunDirection() local result = workspace:Raycast(position, sunDirection * 1000, PARAM) if result then if result.Normal:Dot(sunDirection) < 0 then return true end end return false end, black_and_white = function(color) local average = (color.r + color.g + color.b) / 3 return Color3.new(average, average, average) end } end)() local raytracer_1_0_0 = Instance.new("Tool", owner.Backpack) raytracer_1_0_0.Name = "Raytracer" raytracer_1_0_0.ToolTip = "RAYS : 150 , WIDTH : 10 , HEIGHT : 5" raytracer_1_0_0.Grip = CFrame.new(0.563542724, -0.097826004, 0.377468109, 1, 0, 0, 0, 1, 0, 0, 0, 1) local handle = Instance.new("MeshPart") handle.Name = "Handle" handle.TextureID = "rbxassetid://6318474739" handle.CFrame = CFrame.new(4.85066462, 7.25513029, 5.45907688, 1, 0, 0, 0, 0.999985516, -0.00537854433, 0, 0.00537854433, 0.999985516) handle.CanCollide = false handle.CanQuery = false handle.CanTouch = false handle.Massless = true handle.Rotation = Vector3.new(0.31, 0, 0) handle.Size = Vector3.new(1.31, 0.896, 0.903) handle.Parent = raytracer_1_0_0 function newInput(keycode) local p = Instance.new("ProximityPrompt", handle) p.Style = Enum.ProximityPromptStyle.Custom p.MaxActivationDistance = 3 p.KeyboardKeyCode = keycode return p.Triggered end local cam = handle local rays = 150 local width = 10 local height = 5 local FOV = 7 newInput(Enum.KeyCode.Period):Connect(function(p) if p ~= owner then return end rays += 10 raytracer_1_0_0.ToolTip = `RAYS : {rays} , WIDTH : {width} , HEIGHT : {height}` print(raytracer_1_0_0.ToolTip) end) newInput(Enum.KeyCode.Comma):Connect(function(p) if p ~= owner then return end rays -= 10 raytracer_1_0_0.ToolTip = `RAYS : {rays} , WIDTH : {width} , HEIGHT : {height}` print(raytracer_1_0_0.ToolTip) end) function addColor(colors) local result = Vector3.new() for _, col in colors do result += Vector3.new(col.R, col.G, col.B) end return result end function antiAlias(pixels) local antiAliasedPixels = {} for y, row in pairs(pixels) do antiAliasedPixels[y] = {} for x, pixel in pairs(row) do local topLeft = pixels[y - 1] and pixels[y - 1][x - 1] or pixel local top = pixels[y - 1] and pixels[y - 1][x] or pixel local topRight = pixels[y - 1] and pixels[y - 1][x + 1] or pixel local left = pixels[y] and pixels[y][x - 1] or pixel local right = pixels[y] and pixels[y][x + 1] or pixel local bottomLeft = pixels[y + 1] and pixels[y + 1][x - 1] or pixel local bottom = pixels[y + 1] and pixels[y + 1][x] or pixel local bottomRight = pixels[y + 1] and pixels[y + 1][x + 1] or pixel local sum = addColor{ topLeft, top, topRight, left, right, bottomLeft, bottom, bottomRight } local antiAliasedValue = sum / 8 antiAliasedPixels[y][x] = Color3.new(antiAliasedValue.X, antiAliasedValue.Y, antiAliasedValue.Z) end end return antiAliasedPixels end function colorInThreshold(color1, color2, threshold) threshold = threshold or 0.04 local diff = 0 local s = "RGB" for i = 1, 3 do local index = s:sub(i,i) diff = diff + (color1[index] - color2[index])^2 end diff = math.sqrt(diff) return diff <= threshold end local parts = {_size = nil} raytracer_1_0_0.Activated:Connect(function() local origin = cam.CFrame local direction = cam.CFrame.LookVector local uv = {} local pixels = {} local start = os.clock() local last local lx = 0 local x,y = 0,0 local spacing = width / (rays - 1) local size = spacing local ch = script:GetChildren() --if parts._size ~= size then for i = 1, #ch do if i%300 == 0 then task.wait(0.05) end ch[i]:Destroy() end --end for i = -height/2, height/2, spacing do y += 1 if i % 6 == 0 then task.wait() end if not pixels[y] then pixels[y] = {} end for j = -width/2, width/2, spacing do x += 1 lx += 1 local pos = origin * CFrame.new(j, i, -FOV) local dir = (origin.Position - pos.Position).Unit * -1000 local result = workspace:Raycast(origin.Position, dir) if result then local col = result.Instance.Color local endColor = col:Lerp(Color3.new(), 1 - Utility.voxel_shade(result.Normal, game.Lighting:GetSunDirection())) if Utility.in_shadow(result.Position, result.Instance) then endColor = endColor:Lerp(Color3.new(), 0.4) end pixels[y][x] = endColor last = endColor else local col = Color3.fromRGB(128, 187, 219) local clockTime = game.Lighting.ClockTime local mappedValue = math.max(0, (clockTime - 7) / 10) col = col:Lerp(Color3.new(), 1 - mappedValue) if game.Lighting.ClockTime > 4 then local noise = math.noise(x / 50, y / 40) col = col:Lerp(Color3.new(1,1,1), math.clamp(noise - 0.15 + (math.noise(y/20,x/10) / 20),0,1)) end pixels[y][x] = col last = col end end x = 0 end --print("done raycasting, "..os.clock() - start) start = os.clock() --print("anti-aliasing") pixels = antiAlias(pixels) --print("done AA, "..os.clock() - start) start = os.clock() --print("rendering") parts._size = size for y, xdata in ipairs(pixels) do local last parts[y] = {} for x, result in xdata do if x % 80 == 0 then task.wait() end if last then if colorInThreshold(last.Color, result) then last.Size += Vector3.new(size, 0, 0) last.CFrame *= CFrame.new(size/2, 0, 0) continue end end if size== parts._size and parts[y][x] then parts[y][x].Color = result return end local tst = Instance.new("Part\0") tst.Anchored = true tst.CanCollide = false tst.CanQuery = false tst.Size = Vector3.new(1, 1, 1) * size tst.Color = result tst.CFrame = origin * CFrame.new(x * tst.Size.Z , y * tst.Size.Y, 0) tst.Parent = script parts[y][x] = tst last = tst end end --print("done rendering, "..os.clock() - start) end)